#include "AppealListWidget.h"

AppealListWidget::AppealListWidget(QWidget *parent)
    : QWidget{parent},
      currentPage(1),
      totalPages(1)
{
    // 创建一个主垂直布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建一个 QWidget 作为 评论 的内容容器
    this->container = new QWidget(this);
    this->contentLayout = new QVBoxLayout(this->container);

    // 创建几个测试用的 AppealItemWidget
    QList<Appeal> appeals = DataAccess::getAppealList();
    load(appeals);

    // 设置容器的布局
    this->container->setLayout(contentLayout);

    // 将 评论 添加到主布局中
    mainLayout->addWidget(this->container);

    // 分页器 UI
    QHBoxLayout *paginationLayout = new QHBoxLayout;

    QPushButton *prevButton = new QPushButton("上一页", this);
    QLabel *pageInfoLabel = new QLabel(this);
    QLineEdit *jumpPageEdit = new QLineEdit(this);
    QPushButton *nextButton = new QPushButton("下一页", this);
    QPushButton *reloadButton = new QPushButton("刷新", this);

    jumpPageEdit->setFixedWidth(50);
    jumpPageEdit->setAlignment(Qt::AlignCenter);
    jumpPageEdit->setPlaceholderText("页码");

    pageInfoLabel->setText(QString("第%1页 / 共%2页").arg(this->currentPage).arg(this->totalPages));

    prevButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    nextButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    jumpPageEdit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    pageInfoLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
    reloadButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);

    // 按钮样式
    QString buttonStyle =
        "QPushButton {"
        "   background-color: #007BFF;"
        "   color: white;"
        "   border: none;"
        "   padding: 5px 10px;"
        "   border-radius: 4px;"
        "   font-size: 14px;"
        "}"
        "QPushButton:hover {"
        "   background-color: #0056b3;"
        "}";

    prevButton->setStyleSheet(buttonStyle);
    nextButton->setStyleSheet(buttonStyle);
    reloadButton->setStyleSheet(buttonStyle);

    // 输入框样式
    jumpPageEdit->setStyleSheet(
        "QLineEdit {"
        "   border: 1px solid #ccc;"
        "   border-radius: 4px;"
        "   padding: 2px 5px;"
        "   background-color: #f9f9f9;"
        "   font-size: 14px;"
        "}"
        "QLineEdit:focus {"
        "   border: 1px solid #007BFF;"
        "}");

    // 标签样式
    pageInfoLabel->setStyleSheet(
        "QLabel {"
        "   font-size: 14px;"
        "   color: #333;"
        "   margin: 0 10px;"
        "}");

    paginationLayout->addWidget(prevButton);
    paginationLayout->addWidget(pageInfoLabel);
    paginationLayout->addWidget(jumpPageEdit);
    paginationLayout->addWidget(nextButton);
    paginationLayout->addWidget(reloadButton);

    paginationLayout->insertStretch(0); // 在布局最前面插入一个水平弹簧
    paginationLayout->addStretch();     // 在布局末尾插入一个水平弹簧

    // 将分页器添加到主布局中
    mainLayout->addLayout(paginationLayout);

    mainLayout->addStretch();

    // 设置主布局
    setLayout(mainLayout);
}

void AppealListWidget::load(const QList<Appeal> &appeals)
{
    QLayoutItem *item;
    while ((item = this->contentLayout->takeAt(0)) != nullptr)
    {
        if (item->widget())
            delete item->widget();
        delete item;
    }

    // 添加真正的数据
    int itemCount = appeals.size();
    for (int i = 0; i < itemCount; ++i)
    {
        AppealItemWidget *blogItem = new AppealItemWidget(this->container);
        connect(blogItem, &AppealItemWidget::openMyPage, this, &AppealListWidget::openMyPage);
        blogItem->quickSetup(appeals[i]);
        this->contentLayout->addWidget(blogItem);
    }

    this->contentLayout->addStretch();
}